<?php

namespace App\Console\Commands\Common;

use App\Models\Common\CityTerritorialBoundaryModel;
use Illuminate\Console\Command;

class UpdateBoundary extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'common:update-boundary';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新高德城市边界范围信息';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        CityTerritorialBoundaryModel::query()
            ->chunk(100, function ($cityTerritorialBoundaryModel) {
                foreach ($cityTerritorialBoundaryModel as $v) {
                    list(
                        $maxLongitude,
                        $maxLatitude,
                        $minLongitude,
                        $minLatitude
                        ) = GenerateNationalHousePrice::getAreaBorderline($v->polyline);
                    $boundary = [
                        'maxLongitude' => $maxLongitude,
                        'maxLatitude'  => $maxLatitude,
                        'minLongitude' => $minLongitude,
                        'minLatitude'  => $minLatitude,
                    ];
                    CityTerritorialBoundaryModel::query()->where('id', $v->id)->update(['boundary' => json_encode($boundary)]);
                }
            });
    }
}
